package com.bustle.product.proxy;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;

import com.bustle.commons.JsonParser;
import com.bustle.commons.data.dao.Dao;
import com.bustle.commons.data.dao.DaoProxy;
import com.bustle.commons.http.RestDtoDaoProxy;
import com.bustle.commons.http.TypeToJsonHttpConverter;
import com.bustle.product.dto.product.ProductDto;
import com.bustle.product.dto.product.ProductDtoDaoWebService.ProductDtoList;
import com.bustle.product.dto.product.ProductDtoMapper;

@Local
public interface ProductDtoDaoProxy extends Dao<Integer, ProductDto>
{
  @Stateless
  public class DefaultProductDtoDaoProxy extends DaoProxy<Integer, ProductDto, Dao<Integer, ProductDto>> implements ProductDtoDaoProxy
  {
    @PostConstruct
    public void postBuild()
    {
      dao = new RestDtoDaoProxy<Integer, ProductDto>("localhost", 8080, "product", new JsonParser(), new TypeToJsonHttpConverter<>(ProductDto.class), ProductDto.class, dtoMapper)
        {
          @Override
          protected List<ProductDto> retrieveAllFrom(String aResult)
          {
            return parser.parse(ProductDtoList.class, aResult).getProduct();
          }
        };
    }

    @EJB
    protected ProductDtoMapper dtoMapper;
  }
}
